package com.djinfo.work.activiti.controller;

import com.djinfo.work.activiti.pagemodel.DataGrid;
import com.djinfo.work.activiti.pagemodel.Userinfo;
import com.djinfo.work.activiti.po.*;
import com.djinfo.work.activiti.service.SystemService;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.entity.TbSysRoleUser;
import com.djinfo.work.entity.TbSysRoles;
import com.djinfo.work.entity.TbSysUsers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/system")
public class SystemController {
	@Autowired
	SystemService systemService;
	
	@RequestMapping("/useradmin.do")
	public String useradmin(){
		return "activiti/system/useradmin.html";
	}
	
	@RequestMapping("/roleadmin.do")
	public String roleadmin(){
		return "activiti/system/roleadmin.html";
	}
	
	@RequestMapping("/permissionadmin.do")
	public String permissionadmin(){
		return "activiti/system/permissionadmin.html";
	}

	/**
	 * 用户列表
	 * @param current
	 * @param rowCount
     * @return
     */
	@RequestMapping(value="/userlist.do",method= RequestMethod.POST)
	@ResponseBody
	DataGrid<Userinfo> userlist(@RequestParam("current") int current, @RequestParam("rowCount") int rowCount){
		int total=systemService.getTbSysUsers().size();
		List<TbSysUsers> tbSysUsersList = systemService.getpageusers(current,rowCount);
		List<Userinfo> users=new ArrayList<Userinfo>();
		for(TbSysUsers user:tbSysUsersList){
			Userinfo u=new Userinfo();
			u.setId(user.getPkUserId());
			u.setEmail(user.getEmail());
			u.setPassword(user.getUserPassword());
			u.setTel(user.getPhoneNum());
			u.setUsername(user.getUserName());
			String rolename="";
			List<TbSysRoleUser> tbSysRoleUserList =systemService.getTbSysRoleUser(u.getId());
			if(tbSysRoleUserList!=null){
				for(TbSysRoleUser userole:tbSysRoleUserList){
					TbSysRoles tbSysRoles = (TbSysRoles)systemService.findUniqueByProperty(TbSysRoles.class,"pkRoleId",userole.getRoleId());
					String roleName = tbSysRoles.getRoleName();
					rolename=rolename+","+roleName;
				}
				if(rolename.length()>0)
				rolename=rolename.substring(1,rolename.length());
				
				u.setRolelist(rolename);
			}
			users.add(u);
		}
		DataGrid<Userinfo> grid=new DataGrid<Userinfo>();
		grid.setCurrent(current);
		grid.setRows(users);
		grid.setRowCount(rowCount);
		grid.setTotal(total);
		return grid;
	}

	/**
	 * 用户信息
	 * @param userid
	 * @return
     */
	@RequestMapping(value="/user/{uid}",method= RequestMethod.GET)
	@ResponseBody
	TbSysUsers getuserinfo(@PathVariable("uid") String userid){
		return systemService.getUserByid(userid);
	}

	/**
	 * 角色列表
	 * @return
     */
	@RequestMapping(value="/rolelist.do",method= RequestMethod.POST)
	@ResponseBody
	List<TbSysRoles> getroles(){
		return systemService.loadAll(TbSysRoles.class);
	}

	/**
	 * 获取所有角色
	 * @param current
	 * @param rowCount
     * @return
     */
	@RequestMapping(value="/roles.do",method= RequestMethod.POST)
	@ResponseBody
	DataGrid<TbSysRoles> getallroles(@RequestParam("current") int current, @RequestParam("rowCount") int rowCount){
		List<TbSysRoles> roles=systemService.getallRoleinfo();
		List<TbSysRoles> rows=systemService.getpageRoleinfo(current, rowCount);
		for (TbSysRoles tbSysRoles:rows){
			String rid = tbSysRoles.getPkRoleId();
			List<Role_permission> role_permissionList = systemService.findByProperty(Role_permission.class,"roleid",rid);
			for(Role_permission role_permission:role_permissionList){
				Permission permission =null;
				String permissionid = role_permission.getPermissionid();
				permission = systemService.findUniqueByProperty(Permission.class,"pid",permissionid);
				role_permission.setPermission(permission);
			}
			tbSysRoles.setRole_permissions(role_permissionList);
		}
		DataGrid<TbSysRoles> grid=new DataGrid<TbSysRoles>();
		grid.setCurrent(current);
		grid.setRowCount(rowCount);
		grid.setTotal(roles.size());
		grid.setRows(rows);
		return grid;
	}
	/**
	 * 权限列表
	 * @return
     */
	@RequestMapping(value="permissionlist.do",method= RequestMethod.POST)
	@ResponseBody
	List<Permission> getPermisions(){
		return systemService.loadAll(Permission.class);
	}

	/**
	 *
	 * @param rolename
	 * @param permissionname
     * @return
     */
	@RequestMapping(value="addrole.do",method= RequestMethod.POST)
	public String addrole(@RequestParam("roleName") String rolename, @RequestParam(value="permissionname[]")String[] permissionname, HttpSession session){
		String userid=(String) session.getAttribute("username");
		String nowTime= StringUtil.getNowTime();
		TbSysRoles r=new TbSysRoles();
		r.setRoleName(rolename);
		r.setCreateUser(userid);
		r.setUpdateUser(userid);
		r.setCreateTime(nowTime);
		r.setUpdateTime(nowTime);
		systemService.addSysRoles(r, permissionname);
		return "activiti/system/roleadmin.jsp";
	}

	/**
	 * 删除角色，这不做此功能
	 * @param rid
	 * @return
     */
	@RequestMapping(value="/deleterole.do",method= RequestMethod.POST)
	public String deleterole(String rid){
		systemService.deleterole(rid);
		return "activiti/system/roleadmin.jsp";
	}

	/**
	 * 通过id 查找角色信息。
	 * @param rid
	 * @return
     */
	@RequestMapping(value="roleinfo.do",method= RequestMethod.POST)
	@ResponseBody
	public TbSysRoles getRolebyrid(String rid){
		return systemService.getTbSysRolesbyid(rid);
	}

	/**
	 * 对角色的权限进行修改
	 * @param rid
	 * @param permissionnames
     * @return
     */
	@RequestMapping(value="updaterole.do",method= RequestMethod.POST)
	public String updaterole(String rid, @RequestParam(value="permissionname[]")String[] permissionnames){
		systemService.deleterolepermission(rid);
		systemService.updaterole(rid, permissionnames);
		return "activiti/system/roleadmin.jsp";
	}

	/**
	 * 权限列表分页
	 * @param current
	 * @param rowCount
     * @return
     */
	@RequestMapping(value="permissions.do",method= RequestMethod.POST)
	@ResponseBody
	DataGrid<Permission> getpermissions(@RequestParam("current") int current, @RequestParam("rowCount") int rowCount){
		List<Permission> p=systemService.getPermisions();
		List<Permission> list=systemService.getPagePermisions(current, rowCount);
		DataGrid<Permission> grid=new DataGrid<Permission>();
		grid.setCurrent(current);
		grid.setRowCount(rowCount);
		grid.setTotal(p.size());
		grid.setRows(list);
		return grid;
	}

	/**
	 * 添加权限
	 * @param permissionname
	 * @return
     */
	@RequestMapping(value="addpermission.do",method= RequestMethod.POST)
	public String addpermission(@RequestParam("permissionname") String permissionname){
		Permission permission = new Permission();
		permission.setPermissionname(permissionname);
		systemService.addPermission(permission);
		return "activiti/system/permissionadmin.jsp";
	}
	
	
	@RequestMapping(value="deletepermission.do",method= RequestMethod.POST)
	public String deletepermission(String pid){
		systemService.deletepermission(pid);
		return "activiti/system/permissionadmin.jsp";
	}
}
